if (!Skink.Section) Skink.Section = {};

Skink.Section.init = function() {
    Skink.Section.search = {
        panel: 'panel-search',
        timeline: 'timeline-search',
        wrapper: 'timelinewrapper-search',
        tab: 'tab-search',
        tabIndex: 0,
        urls: new Array('http://search.twitter.com/search.json?q={{query}}'),
        lastid: 0,
        lastcheck: 0,
        currdata: null,
        prevdata: null,
        autoload: false,
        canclear: false,
        mincachetime: 1,

        lastquery: null,

        build: function(force) {
            if ($('#search-for').val().length > 0) {
                // clear the existing results if this is a new query
                if (this.lastquery != $('#search-for').val()) {
                    $('#' + this.timeline + ' .timeline-entry').remove();
                }

                Skink.UI.statusBar("Searching for '" + $('#search-for').val() + "'…");
                Skink.UI.showLoading();

                var url = 'http://summize.com/search.json';
                var data = {
                    "rpp": Skink.Prefs.get('search-maxhits'),
                    "q": $('#search-for').val()
                };
                $.get(url, data, this.onAjaxComplete)
            }
        },

        onAjaxComplete: function(data, msg) {
            Skink.Section.search.lastquery

            var data = parseJSON(data);
            Skink.dump(data);

            var term = data.query;
            Skink.Section.search.lastquery = term;

            if (data && data.results) {
                function summizeToTweet(result) {
					air.trace('tweet:'+JSON.stringify(result));
                    var tweet = {
                        "text": result.text,
                        "created_at": result.created_at,
                        "id": result.id,
                        "in_reply_to_user_id": result.to_user_id,
                        "favorited": false,
                        "source": "web",
                        "truncated": false,
                        "user": {
                            "id": result.from_user_id,
                            "screen_name": result.from_user,
                            "profile_image_url": result.profile_image_url,
                            "protected": false
                        }
                    }

                    // air.trace(JSON.stringify(tweet));
                    return tweet
                }

                Skink.UI.statusBar("Found " + data.results.length + " results for '" + $('#search-for').val() + "'");
                Skink.UI.hideLoading();

                for (var x = 0; x < data.results.length; x++) {
                    Skink.Section.search.addItem(summizeToTweet(data.results[x]))
                }
                Skink.dump('Search section cleaning up timeline');
                Skink.Section.search.cleanup();

                // add search term highlighting
                $('#' + Skink.Section.search.timeline + " div.timeline-entry").each(function() {
                    $.highlight(this, term.toUpperCase());
                });
            }
        },

		addItem: function(item) {
            Skink.UI.addItemToTimeline(item, this)
        },

        cleanup: function(attribute) {
            Skink.UI.cleanupTimeline(this.timeline, true, true, true);
        }
    }

    Skink.Section.prefs = {
        panel: 'panel-prefs',
        timeline: 'timeline-prefs',
        wrapper: '',
        tab: 'tab-prefs',
        tabIndex: 1,
        autoload: false,
        canclear: false,
        build: function(force) {},
        onAjaxComplete: function(url, xhr, msg) {},
        addItem: function(item) {},
        cleanup: function(attribute) {}
    }

    Skink.Section.getSectionFromTab = function(tab) {
        var sectionStr = tab.id.replace(/tab-/, '');
        Skink.dump('section for tab:' + sectionStr);
        return Skink.Section[sectionStr];
    };

    Skink.Section.getSectionFromTimeline = function(timeline) {
        var sectionStr = timeline.id.replace(/timeline-/, '');
        Skink.dump('section for tab:' + sectionStr);
        return Skink.Section[sectionStr];
    };
}